package com.p2p.front.web.controller;

import java.text.ParseException;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.MyPage;
import com.p2p.base.model.BaseUser;
import com.p2p.base.page.PageUtil;
import com.p2p.base.page.PageVo;
import com.p2p.base.util.DateUtil;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.web.form.AccountFreezeQueryForm;
import com.p2p.front.web.form.AccountQueryForm;
import com.p2p.trans.client.enums.AccountTypeEnum;
import com.p2p.trans.client.facade.AccountQueryFacade;
import com.p2p.trans.client.model.AccountFreezeLogQueryDto;
import com.p2p.trans.client.model.AccountLogQueryDto;
import com.p2p.trans.client.result.AccountQueryResult;

/**
 * @author ruiqi
 *
 */
@RequestMapping("/account")
@Controller
public class AccountQueryController extends BaseController {

	private static final int TRANS_CODE_LENGTH = 4;

	private static final Logger logger = LoggerFactory
			.getLogger(AccountQueryController.class);

	@Resource
	private AccountQueryFacade accountQueryFacade;

	@RequestMapping("/viewLog")
	public String viewLog(ModelMap map, AccountQueryForm accountQueryForm) {
		BaseUser user = getCurrentUser();
		String transAccount = user.getAccountNo();
		AccountLogQueryDto accountLogQueryDto = form2Dto(accountQueryForm);

		accountLogQueryDto.setTransAccount(transAccount);

		MyPage page = accountQueryFacade
				.queryLogPageByConditions(accountLogQueryDto);

		map.addAttribute("page", page);

		// page插件转换
		// 分页url带上查询条件
		page.getParams().put("transCode", accountQueryForm.getTransCode());
		page.getParams().putAll(accountQueryForm.getParams());
		page.setStartDate(accountQueryForm.getStartDate());
		page.setEndDate(accountQueryForm.getEndDate());

		AccountQueryResult result = accountQueryFacade
				.getAccountInfo(transAccount);

		if (!result.isSuccess()) {
			map.addAttribute("err", result.getResultCode());
			return "errorPage";
		}
		map.addAttribute("account", result.getAccountVO());
		return "account/viewLog";
	}

	private AccountLogQueryDto form2Dto(AccountQueryForm form) {

		AccountLogQueryDto dto = new AccountLogQueryDto();
		Long minLogId = null;
		if (form.getStartDate() != null
				&& StringUtils.isNotBlank(form.getStartDate())) {
			// String startDateStr =
			// DateUtil.getYYYYMMDDStr(form.getStartDate());
			// String minLogIdStr = String.format("%09d",startDateStr);
			// "2014-01-02"转换成"20140102"
			String startDateStr = form.getStartDate().replaceAll("-", "");
			String minLogIdStr = startDateStr + "000000000";
			minLogId = Long.valueOf(minLogIdStr);
		}

		Long maxLogId = null;
		if (form.getEndDate() != null
				&& StringUtils.isNotBlank(form.getEndDate())) {
			// String endDateStr = DateUtil.getYYYYMMDDStr(form.getEndDate());
			// String maxLogIdStr = String.format("%99d",endDateStr);
			String endDateStr = form.getEndDate().replaceAll("-", "");
			;
			String maxLogIdStr = endDateStr + "999999999";
			maxLogId = Long.valueOf(maxLogIdStr);
		}

		// 拷贝查询条件
		dto.setMinLogId(minLogId);
		dto.setMaxLogId(maxLogId);
		if (StringUtils.isNotBlank(form.getTransCode())) {
			dto.setTransCode(form.getTransCode());
		} else {
			Object paramsTransCode = form.getParams().get("transCode");
			if (paramsTransCode != null) {
				dto.setTransCode(paramsTransCode.toString());
			}
		}
		// 处理subTransCode
		if (StringUtils.isNoneBlank(dto.getTransCode())
				&& dto.getTransCode().length() > TRANS_CODE_LENGTH) {
			dto.setSubTransCode(dto.getTransCode());
			dto.setTransCode(null);
		}
		// 拷贝分页条件
		dto.setPageSize(form.getPageSize());
		dto.setPageNum(form.getPageNum());

		return dto;
	}

	@RequestMapping("/viewFreezeLog")
	public String viewFreezeLog(ModelMap map,
			AccountFreezeQueryForm accountFreezeQueryForm) {
		BaseUser user = getCurrentUser();

		String transAccount = user.getAccountNo();
		AccountFreezeLogQueryDto accountFreezeLogQueryDto = form2Dto(accountFreezeQueryForm);

		accountFreezeLogQueryDto.setAccountNo(transAccount);

		AccountQueryResult result = accountQueryFacade
				.queryFreezeLogPageByConditions(accountFreezeLogQueryDto);
		if (result.isSuccess()) {
			AccountFreezeLogQueryDto accountFreezeLogQueryDto2 = result.getAccountFreezeLogQueryDto();
			PageVo page = new PageVo();
			page.setList(accountFreezeLogQueryDto2.getAccountFreezeLogList());
			page.setPageNum(accountFreezeLogQueryDto2.getPgNumber());
			page.setPageSize(accountFreezeLogQueryDto2.getLimit());
			page.setPages(PageUtil.caculateTotalPage(accountFreezeLogQueryDto2.getTotalCount(), accountFreezeLogQueryDto2.getLimit()));
			map.addAttribute("page", page);
		}
		
		map.addAttribute("accountQueryForm", accountFreezeQueryForm);

		return "account/viewFreezeLog";
	}

	private AccountFreezeLogQueryDto form2Dto(AccountFreezeQueryForm form) {

		AccountFreezeLogQueryDto dto = new AccountFreezeLogQueryDto();

		try {
			dto.setStartTime(form.getStart() == null ? null : DateUtil
					.parseYYYYMMDDStr(form.getStart()));
			dto.setEndTime(form.getEnd() == null ? null : DateUtil
					.parseYYYYMMDDStr(form.getEnd()));
		} catch (ParseException e) {
			logger.error("date format error", e);
			throw new RuntimeException(e);
		}
		// 拷贝分页条件
		dto.setLimit(form.getPageSize());
		dto.setPgNumber(form.getPageNum());

		return dto;
	}

}
